package com.neu.controller;

import com.neu.pojo.Dept;
import com.neu.pojo.User;
import com.neu.service.DeptService;
import com.neu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @RequestMapping("login")
    public ModelAndView login(User user){
        User u = userService.login(user);

        ModelAndView mv = new ModelAndView();
        if(u!=null){
            //登录成功，跳转到主页面
            List<Dept> deptList = deptService.getDeptList();
            mv.addObject("user",u);
            mv.addObject("deptList",deptList);
            mv.setViewName("main");
        }else{
            //登录失败
            mv.addObject("error","用户名或密码错误");
            mv.addObject("user",user);// 数据回显
            mv.setViewName("login");
        }
        return mv;
    }
}
